package com.cjzuji.rental.dao.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjzuji.rental.controller.order.YouPinController;
import com.cjzuji.rental.dao.AiZuJiUserDao;
import com.cjzuji.rental.mapper.OrderUserMapper;
import com.cjzuji.rental.pojo.OrderUser;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class AiZuJiUserDaoImpl implements AiZuJiUserDao {
    private static final Logger logger = LoggerFactory.getLogger(YouPinController.class);
    @Autowired
    OrderUserMapper orderUserMapper;

    @Override
    @Transactional
    public  void save(Map<String,Object> detail, Long orderID) {
        String idcardImgUrl = "";
        String idcardBackImgUrl = "";
        if (detail.get("cardBackUrl") != null){
            idcardImgUrl = detail.get("cardBackUrl").toString(); //身份证正面
        }
        if (detail.get("cardFaceUrl") != null){
            idcardBackImgUrl = detail.get("cardFaceUrl").toString(); //身份证反面
        }
        Map<String,String> orderDetailResp = (Map<String,String>)detail.get("orderDetailResp");
        //System.out.println("orderDetailResp~~~~~~~~~~~~"+orderDetailResp);
        String addressInfo = orderDetailResp.get("prov").toString()+orderDetailResp.get("area").toString()+orderDetailResp.get("city").toString()+orderDetailResp.get("address").toString();

        OrderUser aiZUJiOrderUser = OrderUser.builder()
                .orderId(orderID)
                .userName(orderDetailResp.get("realName").toString())  //收货人
                .mobile(orderDetailResp.get("phoneNum").toString())
                .province(orderDetailResp.get("prov").toString())
                .city(orderDetailResp.get("area").toString())
                .county(orderDetailResp.get("city").toString())
                .address(orderDetailResp.get("address").toString())
                .addressInfo(addressInfo)
                .idCard(orderDetailResp.get("idNo").toString())
                .idCardName(orderDetailResp.get("realName").toString())
                .idCardImg(idcardImgUrl)
                .idCardBlackImg(idcardBackImgUrl)
                .faceImg("")
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        try {
            orderUserMapper.insert(aiZUJiOrderUser);
        }catch (Exception e){
            logger.info("订单用户添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }
}
